import hashlib

from django.conf.urls import url
from django.contrib import messages
from django.shortcuts import render
from .models import *
from django.db.models import Q


class Configuration:
    def __init__(self, CPU, Mboard, GPU, RAM, HDD, SSD, Power, Chassis, Display):
        self.CPU = CPU
        self.Mboard = Mboard
        self.GPU = GPU
        self.RAM = RAM
        self.HDD = HDD
        self.SSD = SSD
        self.Power = Power
        self.Chassis = Chassis
        self.Display = Display


configuration = Configuration(CPU, Mboard, GPU, RAM, HDD, SSD, Power, Chassis, Display)


# Create your views here.
def setPassword(password):
    md5 = hashlib.md5()
    md5.update(password.encode())
    password = md5.hexdigest()
    return str(password)


def indexShow(request):
    request.session.flush()
    return render(request, 'index.html')


def logingoto(request):
    return render(request, 'login.html')


def login(request):
    u = request.POST.get('userName', '')
    p = request.POST.get('userPassword', '')
    if u and p:
        user = User.objects.filter(ID=u, Password=setPassword(p)).count()
        if user >= 1:
            request.session['userName'] = u
            request.session.set_expiry(0)
            return render(request, 'index.html', {'name': u, 'user': user})
        else:
            messages.error(request, '用户名或密码输入错误!')
            return render(request, 'login.html')
            # return render(request, 'registerFailed.html', {'content': "用户名或密码输入错误!"})


def registergoto(request):
    return render(request, 'register.html')


def register(request):
    u = request.POST.get('userName', '')
    p = request.POST.get('userPassword', '')
    rep = request.POST.get('rePassword', '')
    if u and p:
        cnt = User.objects.filter(ID=u).count()
        if cnt >= 1:
            messages.error(request, "用户名已存在！")
            return render(request, 'register.html')
        else:
            if p == rep:
                stu = User(ID=u, Permission='1', Password=setPassword(p))
                stu.save()
                return render(request, 'login.html')
            else:
                messages.error(request, "两次输入的密码不一致！")
                return render(request, 'register.html')


def back(request):
    return render(request, 'login.html')


def game(request):
    gamebook = NoteBook.objects.filter(Q(Position__icontains="游戏"))
    return render(request, 'game.html', {'gamebook': gamebook})


def light(request):
    light = NoteBook.objects.filter(
        Q(Position__icontains="轻薄") | Q(Position__icontains="二合一") | Q(Position__icontains="商务"))
    return render(request, 'light.html', {'light': light})


def work(request):
    light = NoteBook.objects.filter(
        Q(Position__icontains="轻薄") | Q(Position__icontains="二合一") | Q(Position__icontains="商务"))
    return render(request, 'light.html', {'light': light})


def forum(request):
    return render(request, 'forum.html')


def assemble(request):
    return render(request, 'assemble.html')


def information(request, NotebookID):
    note = NoteBook.objects.filter(NotebookID=NotebookID)
    return render(request, 'information.html', {'note': note})


def selectg(request):
    m = request.POST.get('brand')
    a = request.POST.get('price')
    if a == "all":
        b = 0
        c = 999999999
    if len(a) == 6:
        b = 0
        c = 6000
    if len(a) == 9:
        b = int(a[0:4])
        c = int(a[5:9])
    if len(a) == 10:
        b = int(a[0:4])
        c = int(a[5:10])
    if len(a) == 11:
        b = int(a[0:5])
        c = int(a[6:11])
    if a == "more":
        b = 15000
        c = 999999999
    if m == "all":
        gamebook = NoteBook.objects.filter(Q(Position__icontains="游戏"),
                                           Q(Price__gte=b),
                                           Q(Price__lte=c)
                                           )
        return render(request, 'game.html', {'gamebook': gamebook})
    if m == "lenovo":
        try:
            gamebook = NoteBook.objects.filter(Q(Position__icontains="游戏"),
                                               Q(Name__icontains="联想"),
                                               Q(Price__gte=b),
                                               Q(Price__lte=c))
        except:
            pass
        print(b)
        print(c)
        return render(request, 'game.html', {'gamebook': gamebook})
    if m == "xiaomi":
        gamebook = NoteBook.objects.filter(Q(Position__icontains="游戏"),
                                           Q(Name__icontains="=xiaomi"),
                                           Q(Price__gte=b),
                                           Q(Price__lte=c)
                                           )
        return render(request, 'game.html', {'gamebook': gamebook})
    if m == "huawei":
        gamebook = NoteBook.objects.filter(Q(Position__icontains="游戏"),
                                           Q(Name__icontains="huawei"),
                                           Q(Price__gte=b),
                                           Q(Price__lte=c)
                                           )
        return render(request, 'game.html', {'gamebook': gamebook})
    if m == "acer":
        gamebook = NoteBook.objects.filter(Q(Position__icontains="游戏"),
                                           Q(Name__icontains="acer"),
                                           Q(Price__gte=b),
                                           Q(Price__lte=c)
                                           )
        return render(request, 'game.html', {'gamebook': gamebook})
    if m == "asus":
        gamebook = NoteBook.objects.filter(Q(Position__icontains="游戏"),
                                           Q(Name__icontains="华硕") | Q(Name__icontains="rog") | Q(
                                               Name__icontains="堡垒"),
                                           Q(Price__gte=b),
                                           Q(Price__lte=c)
                                           )
        return render(request, 'game.html', {'gamebook': gamebook})
    if m == "msi":
        gamebook = NoteBook.objects.filter(Q(Position__icontains="游戏"),
                                           Q(Name__icontains="msi"),
                                           Q(Price__gte=b),
                                           Q(Price__lte=c)
                                           )
        return render(request, 'game.html', {'gamebook': gamebook})
    if m == "dell":
        gamebook = NoteBook.objects.filter(Q(Position__icontains="游戏"),
                                           Q(Name__icontains="alienware") | Q(Name__icontains="戴尔"),
                                           Q(Price__gte=b),
                                           Q(Price__lte=c)
                                           )
        return render(request, 'game.html', {'gamebook': gamebook})
    if m == "微软":
        gamebook = NoteBook.objects.filter(Q(Position__icontains="游戏"),
                                           Q(Name__icontains="surface"),
                                           Q(Price__gte=b),
                                           Q(Price__lte=c)
                                           )
        return render(request, 'game.html', {'gamebook': gamebook})
    if m == "mech":
        gamebook = NoteBook.objects.filter(Q(Position__icontains="游戏"),
                                           Q(Name__icontains="机械革命"),
                                           Q(Price__gte=b),
                                           Q(Price__lte=c)
                                           )
        return render(request, 'game.html', {'gamebook': gamebook})
    if m == "hp":
        gamebook = NoteBook.objects.filter(Q(Position__icontains="游戏"),
                                           Q(Name__icontains="msi"),
                                           Q(Price__gte=b),
                                           Q(Price__lte=c)
                                           )
        return render(request, 'game.html', {'惠普': gamebook})
    if m == "leigod":
        gamebook = NoteBook.objects.filter(Q(Position__icontains="游戏"),
                                           Q(Name__icontains="雷神"),
                                           Q(Price__gte=b),
                                           Q(Price__lte=c)
                                           )
        return render(request, 'game.html', {'gamebook': gamebook})
    if m == "apple":
        gamebook = NoteBook.objects.filter(Q(Position__icontains="游戏"),
                                           Q(Name__icontains="苹果"),
                                           Q(Price__gte=b),
                                           Q(Price__lte=c)
                                           )
        return render(request, 'game.html', {'gamebook': gamebook})
    if m == "cube":
        gamebook = NoteBook.objects.filter(Q(Position__icontains="游戏"),
                                           Q(Name__icontains="清华同方"),
                                           Q(Price__gte=b),
                                           Q(Price__lte=c)
                                           )
        return render(request, 'game.html', {'gamebook': gamebook})


def selectl(request):
    m = request.POST.get('brand')
    a = request.POST.get('price')
    if a == "all":
        b = 0
        c = 999999999
    if len(a) == 6:
        b = 0
        c = 6000
    if len(a) == 9:
        b = int(a[0:4])
        c = int(a[5:9])
    if len(a) == 10:
        b = int(a[0:4])
        c = int(a[5:10])
    if len(a) == 11:
        b = int(a[0:5])
        c = int(a[6:11])
    if a == "more":
        b = 15000
        c = 999999999
    if m == "all":
        light = NoteBook.objects.filter(
            Q(Position__icontains="轻薄") | Q(Position__icontains="二合一") | Q(Position__icontains="商务"),
            Q(Price__gte=b),
            Q(Price__lte=c)
        )
        return render(request, 'light.html', {'light': light})
    if m == "lenovo":
        light = NoteBook.objects.filter(
            Q(Position__icontains="轻薄") | Q(Position__icontains="二合一") | Q(Position__icontains="商务"),
            Q(Name__icontains="联想") | Q(Name__icontains="Thinkpad"),
            Q(Price__gte=b),
            Q(Price__lte=c))

        return render(request, 'light.html', {'light': light})
    if m == "xiaomi":
        light = NoteBook.objects.filter(
            Q(Position__icontains="轻薄") | Q(Position__icontains="二合一") | Q(Position__icontains="商务"),
            Q(Name__icontains="=xiaomi"),
            Q(Price__gte=b),
            Q(Price__lte=c)
        )
        return render(request, 'light.html', {'light': light})
    if m == "huawei":
        light = NoteBook.objects.filter(
            Q(Position__icontains="轻薄") | Q(Position__icontains="二合一") | Q(Position__icontains="商务"),
            Q(Name__icontains="huawei"),
            Q(Price__gte=b),
            Q(Price__lte=c)
        )
        return render(request, 'light.html', {'light': light})
    if m == "acer":
        light = NoteBook.objects.filter(
            Q(Position__icontains="轻薄") | Q(Position__icontains="二合一") | Q(Position__icontains="商务"),
            Q(Name__icontains="acer"),
            Q(Price__gte=b),
            Q(Price__lte=c)
        )
        return render(request, 'light.html', {'light': light})
    if m == "asus":
        light = NoteBook.objects.filter(
            Q(Position__icontains="轻薄") | Q(Position__icontains="二合一") | Q(Position__icontains="商务"),
            Q(Name__icontains="华硕") | Q(Name__icontains="rog") | Q(Name__icontains="堡垒"),
            Q(Price__gte=b),
            Q(Price__lte=c)
        )
        return render(request, 'light.html', {'light': light})
    if m == "msi":
        light = NoteBook.objects.filter(
            Q(Position__icontains="轻薄") | Q(Position__icontains="二合一") | Q(Position__icontains="商务"),
            Q(Name__icontains="msi"),
            Q(Price__gte=b),
            Q(Price__lte=c)
        )
        return render(request, 'light.html', {'light': light})
    if m == "dell":
        light = NoteBook.objects.filter(
            Q(Position__icontains="轻薄") | Q(Position__icontains="二合一") | Q(Position__icontains="商务"),
            Q(Name__icontains="alienware") | Q(Name__icontains="戴尔"),
            Q(Price__gte=b),
            Q(Price__lte=c)
        )
        return render(request, 'light.html', {'light': light})
    if m == "微软":
        light = NoteBook.objects.filter(
            Q(Position__icontains="轻薄") | Q(Position__icontains="二合一") | Q(Position__icontains="商务"),
            Q(Name__icontains="surface"),
            Q(Price__gte=b),
            Q(Price__lte=c)
        )
        return render(request, 'light.html', {'light': light})
    if m == "mech":
        light = NoteBook.objects.filter(
            Q(Position__icontains="轻薄") | Q(Position__icontains="二合一") | Q(Position__icontains="商务"),
            Q(Name__icontains="机械革命"),
            Q(Price__gte=b),
            Q(Price__lte=c)
        )
        return render(request, 'light.html', {'light': light})
    if m == "hp":
        light = NoteBook.objects.filter(
            Q(Position__icontains="轻薄") | Q(Position__icontains="二合一") | Q(Position__icontains="商务"),
            Q(Name__icontains="msi"),
            Q(Price__gte=b),
            Q(Price__lte=c)
        )
        return render(request, 'light.html', {'light': light})
    if m == "leigod":
        light = NoteBook.objects.filter(
            Q(Position__icontains="轻薄") | Q(Position__icontains="二合一") | Q(Position__icontains="商务"),
            Q(Name__icontains="雷神"),
            Q(Price__gte=b),
            Q(Price__lte=c)
        )
        return render(request, 'light.html', {'light': light})
    if m == "apple":
        light = NoteBook.objects.filter(
            Q(Position__icontains="轻薄") | Q(Position__icontains="二合一") | Q(Position__icontains="商务"),
            Q(Name__icontains="苹果"),
            Q(Price__gte=b),
            Q(Price__lte=c)
        )
        return render(request, 'light.html', {'light': light})
    if m == "cube":
        light = NoteBook.objects.filter(
            Q(Position__icontains="轻薄") | Q(Position__icontains="二合一") | Q(Position__icontains="商务"),
            Q(Name__icontains="清华同方"),
            Q(Price__gte=b),
            Q(Price__lte=c)
        )
        return render(request, 'light.html', {'light': light})


def config(request):
    return render(request, 'config.html')


# CPU#######################################################
def chooseCPU(request):
    cpus = CPU.objects.filter()
    return render(request, 'chooseCPU.html', {'cpus': cpus})


def filterCPU(request):
    Brand = request.POST.get('Brand')
    Platform = request.POST.get('Platform')
    CoreNumbers = request.POST.get('CoreNumbers')
    Slot = request.POST.get('Slot')
    RAMType = request.POST.get('RAMType')
    cpus = CPU.objects.filter(Q(Name__icontains=Brand) & Q(Platform__icontains=Platform)
                              & Q(CoreNumbers__icontains=CoreNumbers) & Q(Slot__icontains=Slot)
                              & Q(RAMType__icontains=RAMType))
    return render(request, 'chooseCPU.html', {'cpus': cpus})


def configReturnCPU(request):
    Name = request.POST.get('id')
    cpus = CPU.objects.filter(Q(Name=Name))
    configuration.CPU = cpus[0]
    return render(request, 'config.html', {'configuration': configuration})


def moreInfoCPU(request):
    Name = request.POST.get('id')
    cpus = CPU.objects.filter(Q(Name=Name))
    return render(request, 'moreInfoCPU.html', {'cpu': cpus[0]})


# Mboard#######################################################
def chooseMboard(request):
    cases = Mboard.objects.filter()
    return render(request, 'chooseMboard.html', {'cases': cases})


def filterMboard(request):
    Brand = request.POST.get('Brand')
    CPUType = request.POST.get('CPUType')
    Size = request.POST.get('Size')
    Chipset = request.POST.get('Chipset')
    MemoryType = request.POST.get('MemoryType')
    GraphicsType = request.POST.get('GraphicsType')
    cases = Mboard.objects.filter(
        Q(Name__icontains=Brand) & Q(CPUType__icontains=CPUType) & Q(Size__icontains=Size) & Q(
            Chipset__icontains=Chipset) & Q(MemoryType__icontains=MemoryType) & Q(GraphicsType__icontains=GraphicsType))
    return render(request, 'chooseMboard.html', {'cases': cases})


def configReturnMboard(request):
    Name = request.POST.get('id')
    cases = Mboard.objects.filter(Q(Name=Name))
    configuration.Mboard = cases[0]
    return render(request, 'config.html', {'configuration': configuration})


def moreInfoMboard(request):
    Name = request.POST.get('id')
    cases = Mboard.objects.filter(Q(Name=Name))
    return render(request, 'moreInfoMboard.html', {'case': cases[0]})


# GPU#######################################################
def chooseGPU(request):
    cases = GPU.objects.filter()
    return render(request, 'chooseGPU.html', {'cases': cases})


def filterGPU(request):
    Brand = request.POST.get('Brand')
    GPUType = request.POST.get('GPUType')
    GRAMCapacity = request.POST.get('GRAMCapacity')
    GRAMType = request.POST.get('GRAMType')
    cases = GPU.objects.filter(
        Q(Brand__icontains=Brand) & Q(GPUType__icontains=GPUType) & Q(GRAMCapacity__icontains=GRAMCapacity) & Q(
            GRAMType__icontains=GRAMType))
    return render(request, 'chooseGPU.html', {'cases': cases})


def configReturnGPU(request):
    Name = request.POST.get('id')
    cases = GPU.objects.filter(Q(Name=Name))
    configuration.GPU = cases[0]
    return render(request, 'config.html', {'configuration': configuration})


def moreInfoGPU(request):
    Name = request.POST.get('id')
    cases = GPU.objects.filter(Q(Name=Name))
    return render(request, 'moreInfoGPU.html', {'case': cases[0]})


# RAM#######################################################
def chooseRAM(request):
    cases = RAM.objects.filter()
    return render(request, 'chooseRAM.html', {'cases': cases})


def filterRAM(request):
    Brand = request.POST.get('Brand')
    Platform = request.POST.get('Platform')
    Capacity = request.POST.get('Capacity')
    Type = request.POST.get('Type')
    cases = RAM.objects.filter(
        Q(Name__icontains=Brand) & Q(Platform__icontains=Platform) & Q(Capacity__icontains=Capacity) & Q(
            Type__icontains=Type))
    return render(request, 'chooseRAM.html', {'cases': cases})


def configReturnRAM(request):
    Name = request.POST.get('id')
    cases = RAM.objects.filter(Q(Name=Name))
    configuration.RAM = cases[0]
    return render(request, 'config.html', {'configuration': configuration})


def moreInfoRAM(request):
    Name = request.POST.get('id')
    cases = RAM.objects.filter(Q(Name=Name))
    return render(request, 'moreInfoRAM.html', {'case': cases[0]})


# HDD#######################################################
def chooseHDD(request):
    cases = HDD.objects.filter()
    return render(request, 'chooseHDD.html', {'cases': cases})


def filterHDD(request):
    Brand = request.POST.get('Brand')
    Platform = request.POST.get('Platform')
    Capacity = request.POST.get('Capacity')
    cases = HDD.objects.filter(Q(Name__icontains=Brand) & Q(Platform__icontains=Platform) & Q(Capacity=Capacity))
    return render(request, 'chooseHDD.html', {'cases': cases})


def configReturnHDD(request):
    Name = request.POST.get('id')
    cases = HDD.objects.filter(Q(Name=Name))
    configuration.HDD = cases[0]
    return render(request, 'config.html', {'configuration': configuration})


def moreInfoHDD(request):
    Name = request.POST.get('id')
    cases = HDD.objects.filter(Q(Name=Name))
    return render(request, 'moreInfoHDD.html', {'case': cases[0]})


# SSD#######################################################
def chooseSSD(request):
    cases = SSD.objects.filter()
    return render(request, 'chooseSSD.html', {'cases': cases})


def filterSSD(request):
    Brand = request.POST.get('Brand')
    Capacity = request.POST.get('Capacity')
    Slot = request.POST.get('Slot')
    cases = SSD.objects.filter(Q(Name__icontains=Brand) & Q(Capacity__icontains=Capacity) & Q(Slot__icontains=Slot))
    return render(request, 'chooseSSD.html', {'cases': cases})


def configReturnSSD(request):
    Name = request.POST.get('id')
    cases = SSD.objects.filter(Q(Name=Name))
    configuration.SSD = cases[0]
    return render(request, 'config.html', {'configuration': configuration})


def moreInfoSSD(request):
    Name = request.POST.get('id')
    cases = SSD.objects.filter(Q(Name=Name))
    return render(request, 'moreInfoSSD.html', {'case': cases[0]})


# Power#######################################################
def choosePower(request):
    cases = Power.objects.filter()
    return render(request, 'choosePower.html', {'cases': cases})


def filterPower(request):
    Brand = request.POST.get('Brand')
    power = request.POST.get('Power')
    Authentication = request.POST.get('Authentication')
    cases = Power.objects.filter(
        Q(Name__icontains=Brand) & Q(Power__icontains=power) & Q(Authentication__icontains=Authentication))
    return render(request, 'choosePower.html', {'cases': cases})


def configReturnPower(request):
    Name = request.POST.get('id')
    cases = Power.objects.filter(Q(Name=Name))
    configuration.Power = cases[0]
    return render(request, 'config.html', {'configuration': configuration})


def moreInfoPower(request):
    Name = request.POST.get('id')
    cases = Power.objects.filter(Q(Name=Name))
    return render(request, 'moreInfoPower.html', {'case': cases[0]})


# Chassis#################################################
def chooseChassis(request):
    cases = Chassis.objects.filter()
    return render(request, 'chooseChassis.html', {'cases': cases})


def filterChassis(request):
    Brand = request.POST.get('Brand')
    Capacity = request.POST.get('Capacity')
    if Capacity == "":
        cases = Chassis.objects.filter(Q(Name__icontains=Brand))
    else:
        cases = Chassis.objects.filter(Q(Name__icontains=Brand) & Q(Capacity=Capacity))
    return render(request, 'chooseChassis.html', {'cases': cases})


def configReturnChassis(request):
    Name = request.POST.get('id')
    cases = Chassis.objects.filter(Q(Name=Name))
    configuration.Chassis = cases[0]
    return render(request, 'config.html', {'configuration': configuration})


def moreInfoChassis(request):
    Name = request.POST.get('id')
    cases = Chassis.objects.filter(Q(Name=Name))
    return render(request, 'moreInfoChassis.html', {'case': cases[0]})


# Display###############################################################
def chooseDisplay(request):
    cases = Display.objects.filter()
    return render(request, 'chooseDisplay.html', {'cases': cases})


def filterDisplay(request):
    Brand = request.POST.get('Brand')
    Resolution = request.POST.get('Resolution')
    cases = Display.objects.filter(
        Q(Name__icontains=Brand) & Q(Resolution__icontains=Resolution))
    return render(request, 'chooseDisplay.html', {'cases': cases})


def configReturnDisplay(request):
    Name = request.POST.get('id')
    cases = Display.objects.filter(Q(Name=Name))
    configuration.Display = cases[0]
    return render(request, 'config.html', {'configuration': configuration})


def moreInfoDisplay(request):
    Name = request.POST.get('id')
    cases = Display.objects.filter(Q(Name=Name))
    return render(request, 'moreInfoDisplay.html', {'case': cases[0]})


def userInfo(request):
    return render(request, 'userInfo.html')
